package test.marui.com.mytest.ui.viewPagerTest;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;

import test.marui.com.mytest.R;

/**
 * Created by MR on 2019/2/21.
 */
public class ListFragment extends Fragment {

    private ArrayList<String> mList;
    private Context context;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
        mList = getArguments().getStringArrayList("list");
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_list, container, false);

        initView(rootView);

        return rootView;
    }

    private void initView(View rootView) {
        ListView ls = rootView.findViewById(R.id.ls);
        ArrayAdapter<String> adapter = new ArrayAdapter<>(context, R.layout.item_list, mList);
        ls.setAdapter(adapter);
    }

    public static ListFragment newInstance(ArrayList<String> list) {
        ListFragment fragment = new ListFragment();
        Bundle bundle = new Bundle();
        bundle.putStringArrayList("list", list);
        fragment.setArguments(bundle);
        return fragment;
    }
}
